<template>
  <a-row type="flex" justify="center">
    <a-col :span="20">
      <a-card
        title="资源公共池"
      >
        <div class="table-page-search-wrapper">
          <a-form-model
            layout="inline"
            :model="queryParam"
          >
            <a-row :gutter="15">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="资源所属公司">
                  <a-select v-model="queryParam['filter[company]']" placeholder="选择资源所属公司">
                    <a-select-option v-for="(item,index) in companyOpts" :key="index" :value="index">
                      {{item}}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="联系电话">
                  <a-input v-model="queryParam['filter[phone]']"/>
                </a-form-model-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="业务名称">
                    <a-input v-model="queryParam['filter[b_name]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="入库时间">
                    <a-range-picker @change="onTime1" />
                  </a-form-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="业务分类">
                    <a-select v-model="queryParam['filter[first_type]']" :default-value="0">
                      <a-select-option v-for="(item,index) in firstTypeOpts" :key="index" :value="index">
                        {{item}}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="搜索引擎">
                    <a-input v-model="queryParam['filter[search_engine]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="首次页面">
                    <a-input v-model="queryParam['filter[first_url]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="按搜索词">
                    <a-input v-model="queryParam['filter[search_keywords]']"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-item label="搜索的IP">
                    <a-input v-model="queryParam['filter[ip]']"/>
                  </a-form-item>
                </a-col>
              </template>
              <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                  <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
              </a-col>
            </a-row>
          </a-form-model>
        </div>
        <div class="table-operator">
          <s-table
            bordered
            ref="table"
            size="middle"
            :row-key="(record) => record.id"
            :columns="poolColumns"
            :data="poolData"
          >
            <template slot="title">
              <a-input type="text" />
            </template>
            <template slot="ip" slot-scope="text,record">
              {{text}}<br>
              {{record.ip_format}}
            </template>
            <template slot="company" slot-scope="text">
              {{companyOpts[text]}}
            </template>
          </s-table>
        </div>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
  import { STable } from '@/components'
  import customerColumns from '@/columns/customer'
  import { poolList } from '@/api/customer'

  export default {
    components: {
      STable
    },
    name: 'PoolList',
    data () {
      return {
        poolColumns: customerColumns.all_list2,
        poolData: parameters => {
          return poolList(Object.assign(parameters, this.queryParam)).then(res => {
            this.companyOpts = res.result.company_opts
            this.firstTypeOpts = res.result.first_type_opts
            return res.result
          })
        },
        queryParam: {},
        advanced: false,
        companyOpts: {},
        firstTypeOpts: {}
      }
    },
    created () {
      this.queryParam['filter[company]'] = 'all'
    },
    methods: {
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetForm () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onTime1 (date, dateString) {
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      }
    }
  }
</script>

<style scoped>

</style>
